import { observable, action } from 'mobx';
import { WFBatchToDocIndexStore } from './batchToDocIndex';
import * as API from '../apis/batchToDoc';
import { WeaTableNew } from 'comsMobx'
const { TableStore } = WeaTableNew;
import isEmpty from 'lodash/isEmpty';

export class WFBatchToDocResultStore extends WFBatchToDocIndexStore{

    @observable showSearchAd = false;
    @observable tableStore = new TableStore();
    hasRight = false;

    constructor(){
        super();
        this.copyIndexStore = this.copyIndexStore.bind(this);
        this.setShowSearchAd = this.setShowSearchAd.bind(this);
        this.doSearch = this.doSearch.bind(this);
        this.doBatchToDoc = this.doBatchToDoc.bind(this);
        this.reset = this.reset.bind(this);
        this.rewriteTableStore();
    }

    rewriteTableStore = () =>{
        const getDatas = this.tableStore.getDatas.bind(this.tableStore);//重写组件getdatas方法，处理点击列排序、切页时不走回调
        this.tableStore.getDatas = (...arg) => {
            getDatas(arg[0], arg[1], arg[2], arg[3], (...res) => {
                this.showUnoperators(arg[4]);
            }, arg[5]);
        }
    }


    showUnoperators(callback){
        if(callback && typeof(callback) === 'function'){
            callback();//原tablestore的callback
        }
        let newDatas = [];//[{requestid:1,userid:1,unoperators:1},{requestid:2,userid:2,unoperators:2}]
        if(this.tableStore.datas){
            this.tableStore.datas.map(data => {
                newDatas.push({"requestid":data.requestid,"userid":data.userid,"unoperators":data.unoperators});
            })
        }
        API.getUnoperators({datas:JSON.stringify(newDatas),actiontype:'batch',showAll:'false'}).then(unoperators =>{
            for(var unopt in unoperators){
                jQuery("#"+unopt).html(unoperators[unopt]);
            }
        });
    }

    copyIndexStore(store){
        if(isEmpty(store.fixedCondition)){  //F5刷新
            this.loadCondition();
        }else{  //复制导航条件
            this.fixedCondition = store.fixedCondition;
            this.fixedForm.initFormFields(store.fixedCondition);
            const fixedValues = store.fixedForm && store.fixedForm.getFormDatas() || {};
            this.fixedForm.updateFields(fixedValues, true);
        }
    }

    setShowSearchAd(bool) {
        this.showSearchAd = bool;
    }

    doSearch(params = {}, clearSelected=false){
        this.setShowSearchAd(false);
        const fixedSearchParams = this.fixedForm ? this.fixedForm.getFormParams() : {};
        this.loading = true;
        API.getListResult({...params, ...fixedSearchParams}).then(datas =>{
            this.loading = false;
            if(clearSelected)
                this.tableStore.tableUpdate({selectedRowKeys:[], selectedRowKeysAllPages:[]});
            this.hasRight = datas.hasRight;
            this.tableStore.getDatas(datas.sessionkey, params.current || 1, "", params.sortParams || []);
        });
    }

    doBatchToDoc(){
        let requestids = "";
        const selectedRowKeys = this.tableStore && this.tableStore.selectedRowKeys;
        selectedRowKeys && selectedRowKeys.map(key =>{
            requestids += `${key},`;
        });
        this.loading = true;
        API.doWorkflowToDoc({requestids}).then(datas =>{
            this.loading = false;
            this.doSearch({}, true);
        });
    }

    reset(){
        this.urlParams = {};
        this.showSearchAd = false;
        this.tableStore = new TableStore();
        this.rewriteTableStore();
        this.resetCondition(true);
    }

}